package electionView;

import java.util.concurrent.Semaphore;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import electionCtrl.Election;

public class ElectionGui {
	
	protected JFrame mainFrame;
	protected Election election;
	protected Semaphore semaphore;
	
	public ElectionGui(Election election)
	{
		this(election, new JFrame());
	}
	
	public ElectionGui(Election election, JFrame mainFrame)
	{
		this(election, mainFrame, null);
	}
	
	public ElectionGui(Election election, JFrame mainFrame, Semaphore semaphore)
	{
		this.election = election;
		this.mainFrame = mainFrame;
		this.mainFrame.setResizable(false);
		this.mainFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.semaphore = semaphore;
	}
	
	/**
	 * This shows windows. 
	 **/
	public void showWindow()
	{
		this.mainFrame.setVisible(true);
	}
	
	public void hideWindow()
	{
		this.mainFrame.setVisible(false);
	}
	
	public void enable()
	{
		this.mainFrame.setEnabled(true);
	}
	
	public void disable()
	{
		this.mainFrame.setEnabled(false);
	}

	protected boolean releaseResources()
	{
		if(this.semaphore.availablePermits() > 0)
		{
			return false;
		}
		this.semaphore.release();
		return true;
	}
	
	protected void acquireResources() throws InterruptedException
	{
		this.semaphore.acquire();
	}
}
